package com.company;


import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//使用Java API来编译Java源代码有非常多方法，目前让我们来看一种最简单的方法，通过JavaCompiler进行编译。
//https://gitee.com/CreateQi/java-compiler-test
public class Main {

    public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        // 获取Java编译工具
        JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();

        StandardJavaFileManager manager = systemJavaCompiler.getStandardFileManager(null, null, null);

        File file = new File("E:\\boot_study\\IDEA\\java_study\\src\\com\\company\\SayHello.java");

        Iterable iterable = manager.getJavaFileObjects(file);

        JavaCompiler.CompilationTask task = systemJavaCompiler.getTask(null, manager, null, null, null, iterable);

        task.call();
        manager.close();

        MyClassLoader myClassLoader = new MyClassLoader();
        Class simpleClass = myClassLoader.findClass("SayHello");
        System.out.println(simpleClass);

//        System.out.println(MyClassLoader.class.getName());

        Constructor constructor = simpleClass.getConstructor(null);
        SimpleAction o = (SimpleAction)constructor.newInstance();
        o.sayHello();
//        String path = MyClassLoader.class.getResource("").getPath();
//        /E:/boot_study/IDEA/java_study/out/production/java_study/com/company/
//        System.out.println(path);


//        String name = MyClassLoader.class.getPackage().getName();
        //com.company
//        System.out.println(name);
    }
}
